<template>
  <!-- 货物调仓详情修改对话框 -->
  <el-dialog title="修改" :visible.sync="openWarehouseMoveDetailEdit" width="800px" append-to-body :destroy-on-close="true" :before-close="beforeClose">
    <el-form ref="formParams" :model="formParams" :rules="rules" label-width="120px" :inline="true">
      <el-row>
        <el-col :span="12">
          <el-form-item label="旧仓库条码:" prop="sptmc">
             <el-input
                v-model="formParams.sptmc"
                :disabled="true"
              />
          </el-form-item>
        </el-col>
         <el-col :span="12">
          <el-form-item label="新仓库条码:" prop="sptmc2">
            <el-input
                v-model="formParams.sptmc2"
                :disabled="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="初次日期：" prop="ssrqid">
            <el-input
                v-model="formParams.ssrqid"
                :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物品规格：" prop="spzs">
            <el-input
              v-model="formParams.spzs"
              :disabled="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="品牌：" prop="spcd">
             <el-input
              v-model="formParams.spcd"
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户条码：" prop="sptm">
            <el-input
              v-model="formParams.sptm"
              :disabled="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="柜号:" prop="spgh">
           <el-input
              v-model="formParams.spgh"
              :disabled="true"
            />
        </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批号:" prop="spbh">
            <el-input
              v-model="formParams.spbh"
              :disabled="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
         <el-col :span="12">
          <el-form-item label="旧仓库:" prop="spckmco">
            <el-input
              v-model="formParams.spckmco"
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="旧仓位:" prop="ckcwo">
             <el-input
              v-model="formParams.ckcwo"
              :disabled="true"
            />
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="新仓库:" prop="spckmcn">
            <el-input
              v-model="formParams.spckmcn"
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="新仓位" prop="spcwid">
            <el-select
                v-model="formParams.ckcw"
                @change="changePosition"
                placeholder="请选择新仓位"
                clearable
                :rules="rules.spcwid"
                filterable
                :loading="selectPosition3Loading"
              >
                <el-option
                    v-for="item in warehouseSelectPositonList"
                    :key="item.liid"
                    :label="item.ckcw"
                    :value="item.liid"
                />
            </el-select>
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="调拨件数" prop="spjs">
            <el-input
              v-model="formParams.spjs"
              placeholder="请输入调拨件数"
              clearable
              :rules="rules.spjs"
              @input="getWeight(3)"
              type="number"
              min="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单重(Kg)" prop="spjsl">
            <el-input
              v-model="formParams.spjsl"
              :disabled="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="调拨重量(吨)：" prop="spsl">
            <el-input
              v-model="formParams.spsl"
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调拨日期" prop="ssrqid">
            <el-date-picker
                v-model="formParams.ssrqid"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="请输入调拨件数">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注:" prop="itbz">
            <el-input
            v-model="formParams.itbz"
            placeholder="请输入备注"
            clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEdit('formParams')" v-hasPermi="['zhcloud:hwtc:modify']">修 改</el-button>
      </div>
  </el-dialog>
</template>

<script>
import { warehouseMovefModify, selectPosition3 } from "@/api/storage/commonGoods/warehouseMove";
import { WEIGHT_DIGIT } from '@/utils/constant'

export default {
  name: "WarehouseMoveDetailEidt",
  data() {
    return {
      //仓库加载
      selectPosition3Loading: true,
       // 表单校验
      rules: {
        spckmcn: [
          { required: true, message: "新仓库不能为空", trigger: "blur" }
        ],
        ckcwn: [
          { required: true, message: "新仓位不能为空", trigger: "blur" }
        ],
        spjs: [
          { required: true, message: "调仓不能为空", trigger: "blur" },
          { required: true, validator: this.checkSpjs, trigger: "blur" }
        ]
      },
      //弹窗状态
      openWarehouseMoveDetailEdit: false,
      //显示参数
      formParams:{},
      // 提交参数
      submitParams: {
        dzid: "",
        itbz: "",
        spbhid1: "",
        spbhid2: "",
        spckid: "",
        spckid1: "",
        spckid2: "",
        spcwid: "",
        spcwid1: "",
        spcwid2: "",
        spdzld1: "",
        spdzld2: "",
        spjs: "",
        spjs0: "",
        spjsl: "",
        ssrqid: ""
      },
      //选择仓库入参
      selectPositionQuery:{
        spckid:"",
        spcwido:""
      },
      warehouseSelectPositonList:[]
    };
  },
  methods: {
    /**选择仓位 */
    selectPosition() {
      this.selectPosition3Loading = true
      selectPosition3(this.selectPositionQuery).then(response => {
        this.warehouseSelectPositonList = response.data
        this.selectPosition3Loading = false
      });
    },
    /**修改 */
    handleWarehouseMovefModify() {
      this.$modal.loading("数据正在修改中，请稍等...")
      warehouseMovefModify(this.submitParams).then(response => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('修改成功！')
        this.close()
        this.$emit('refresh', true)
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },
    /**点击修改 */
    handleEdit(formName) {
       this.$refs[formName].validate((valid, data) => {
        if(valid) {
          this.$modal.confirm("是否修改？").then(()=> {
            const submitParams = this.submitParams
            const formParams = this.formParams
            for(let i in submitParams) {
              submitParams[i] = formParams[i]
            }
            this.handleWarehouseMovefModify()
          }).catch(() => {})
        } else {
          for(let i in data) {
            const item = data[i]
            for(let j in item) {
              const message = item[j].message
              return this.$modal.msgError(message)
            }
          }
          return false
        }
      })
    },
     /**计算重量和精确到3位 */
    getWeight(n) {
      const spjsl = parseFloat(this.formParams.spjsl)
      let spjs = this.formParams.spjs
      if(spjs || spjs == 0) {
        if(spjs.indexOf('.')>0){
          spjs=spjs.slice(0,spjs.indexOf('.')+ (parseInt(n) + 1))
        }
        this.formParams = {
          ...this.formParams,
          spjs: spjs,
          spsl: parseFloat((spjs*spjsl/1000).toFixed(WEIGHT_DIGIT))
        }
      }
    },
    //选择仓位
    changePosition(data) {
      this.formParams.spcwid = data
    },
    /**验证调拨件数 */
    checkSpjs(rule, value, callback) {
      const sumSpjs = parseFloat(this.formParams.sumSpjs)
      if(sumSpjs < value) {
        callback("调拨件数不可超过原库存件数")
      } else {
        callback()
      }
    },
    /**关闭弹窗 */
    beforeClose(done) {
      this.close()
      done()
    },
    /**关闭窗口 */
    close() {
      this.changeWarehouseMoveDetailEdit()
    },
    /**改变窗口状态 */
    changeWarehouseMoveDetailEdit() {
      this.openWarehouseMoveDetailEdit = !this.openWarehouseMoveDetailEdit;
    },

    /**打开弹窗 */
    open(data, dzid) {
      this.changeWarehouseMoveDetailEdit()
      this.formParams = data
      this.formParams = {
        ...this.formParams,
          spbhid1:data.spbhid,
          spbhid2:data.spbhid2,
          spckid1:data.spckido,
          spckid2:data.spckidn,
          spcwid:Number(data.spcwidn),
          spcwid1:data.spcwido,
          spcwid2:data.spcwidn,
          spdzld1:data.spdzld,
          spjs0:data.spjs,
          dzid:dzid,
          ckcw:data.ckcwn
      }
      this.selectPositionQuery = {
        ...this.selectPositionQuery,
        spckid:data.spckidn,
        spcwido:data.spcwido
      }
      this.selectPosition()
    },
  }
};
</script>
